package service

import (
	"context"
	"fmt"
	"github.com/gin-gonic/gin"
	"math/rand"
	"net/http"
	"strconv"
	"weibo_1006/global"
	"weibo_1006/internal/model"
	"weibo_1006/internal/params"
	"weibo_1006/pkg"
)

func RegisterUser(c *gin.Context) {
	var req params.RegisterUserForm

	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    100000,
			"message": "验证失败",
			"data":    "",
		})
		return
	}

	get := global.RDB.Get(context.Background(), "SendSms"+req.Mobile+"register")

	if get.Val() != req.Sms {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    100000,
			"message": "验证码输入错误,请重新输入",
			"data":    "",
		})
		return
	}

	var user model.User

	user.Mobile = req.Mobile
	err := user.LoginUser()
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    100000,
			"message": "注册失败",
			"data":    err.Error(),
		})
		return
	}

	//手机号验证
	if user.Id > 0 {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    100000,
			"message": "手机号已被注册",
			"data":    "",
		})
		return
	}

	//加盐
	salt := strconv.Itoa(rand.Intn(9000) + 10)
	user.Salt = salt
	//加密
	user.Password = pkg.Md5(req.Password + salt)
	//注册
	err = user.RegisterUser()
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    100000,
			"message": "注册失败",
			"data":    err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code":    http.StatusOK,
		"message": "注册成功",
		"data":    user.Id,
	})
}

func LoginUser(c *gin.Context) {
	var req params.LoginUserForm

	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    100000,
			"message": "验证失败",
			"data":    "",
		})
		return
	}

	var user model.User

	user.Mobile = req.Mobile
	err := user.LoginUser()
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    100000,
			"message": "登陆失败",
			"data":    err.Error(),
		})
		return
	}

	if user.Id == 0 {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    100000,
			"message": "账号或密码错误",
			"data":    "",
		})
		return
	}

	if user.Password != pkg.Md5(req.Password+user.Salt) {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    100000,
			"message": "账号或密码错误",
			"data":    "",
		})
		return
	}

	token := pkg.CreateJwtToken(user.Id)
	c.JSON(http.StatusOK, gin.H{
		"code":    http.StatusOK,
		"message": "登陆成功",
		"data":    token,
	})
}

func AddUserInfo(c *gin.Context) {
	value, _ := c.Get("user_id")

	id, _ := strconv.Atoi(fmt.Sprintf("%d", value))

	var req params.AddUserInfoForm

	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    100000,
			"message": "验证失败",
			"data":    "",
		})
		return
	}
	var info model.UserInfo

	info = model.UserInfo{
		UserId:   int32(id),
		Nickname: req.Nickname,
		Email:    req.Email,
	}

	err := info.AddInfo()
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    100000,
			"message": "添加失败",
			"data":    "",
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code":    http.StatusOK,
		"message": "添加成功",
		"data":    info.Id,
	})
}
